// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   CMICategory.java

package com.iphaeton.lms.course.scorm12.datamodel.cmi;

import com.iphaeton.lms.course.scorm12.DebugIndicator;
import com.iphaeton.lms.course.scorm12.datamodel.DataModelValidator;
import com.iphaeton.lms.course.scorm12.datamodel.Element;
import java.io.PrintStream;
import java.io.Serializable;
import java.lang.reflect.*;

// Referenced classes of package com.iphaeton.lms.course.scorm12.datamodel.cmi:
//            DMErrorManager

public class CMICategory
    implements Serializable
{

    public CMICategory(boolean writeableFlag)
    {
        writeable = writeableFlag;
    }

    public boolean isWriteable()
    {
        return writeable;
    }

    public String determineElementValue(Object o, String element, DMErrorManager dmErrorMgr)
    {
        String rtrnString = new String("");
        Class c = o.getClass();
        try
        {
            Field tmpField = c.getField(element);
            Element e = (Element)tmpField.get(o);
            if(e.isReadable())
            {
                rtrnString = e.getValue();
            } else
            {
                if(_Debug)
                {
                    System.out.println("Error - Element not readable");
                    System.out.println("Value: " + element + " is not readable");
                }
                dmErrorMgr.SetCurrentErrorCode("404");
            }
        }
        catch(NoSuchFieldException nsfe)
        {
            if(element.equals("_count"))
            {
                if(_Debug)
                {
                    System.out.println("Error - Element not an array");
                    System.out.println("Element Cannont have count");
                }
                dmErrorMgr.SetCurrentErrorCode("203");
            } else
            {
                if(_Debug)
                {
                    System.out.println("Error - Invalid Argument Error");
                    System.out.println("Invalid data model element: " + element);
                }
                dmErrorMgr.SetCurrentErrorCode("201");
            }
        }
        catch(SecurityException se)
        {
            if(_Debug)
            {
                System.out.println(se);
                System.out.println("Access to the information is denied");
            }
            dmErrorMgr.SetCurrentErrorCode("101");
        }
        catch(IllegalAccessException iae)
        {
            if(_Debug)
            {
                System.out.println(iae);
                System.out.println("Underlying constructor is inaccessible");
            }
            dmErrorMgr.SetCurrentErrorCode("101");
        }
        return rtrnString;
    }

    public void doSet(Object o, String element, String value, DMErrorManager dmErrorMgr)
    {
        if(_Debug)
        {
            System.out.println("CMICategory::doSet()");
            System.out.println("Object: " + o.getClass().getName());
            System.out.println("Element: " + element);
            System.out.println("Value: " + value);
        }
        Class c = o.getClass();
        try
        {
            Field tmpField = c.getField(element);
            Element e = (Element)tmpField.get(o);
            if(e.isImplemented())
            {
                if(e.isWriteable())
                {
                    if(validateType(o, e, element, value, dmErrorMgr))
                    {
                        e.setValue(value);
                        dmErrorMgr.SetCurrentErrorCode("0");
                        if(_Debug)
                            System.out.println("Element: " + element + " was set to [" + value + "]");
                    } else
                    {
                        if(_Debug)
                        {
                            System.out.println("*** INVALID LMSSetValue() CALL ***");
                            System.out.println("          Invalid Type            ");
                            System.out.println("Element: " + element + " was NOT set!");
                        }
                        dmErrorMgr.SetCurrentErrorCode("405");
                    }
                } else
                {
                    if(_Debug)
                    {
                        System.out.println("Error - Element not Writeable");
                        System.out.println("Element: " + element + " is not writeable");
                    }
                    dmErrorMgr.SetCurrentErrorCode("403");
                }
            } else
            if(e.isMandatory())
            {
                if(_Debug)
                {
                    System.out.println("Error - Mandatory Element not Implemented");
                    System.out.println("Mandatory Value: " + element + " was not implemented");
                }
                dmErrorMgr.SetCurrentErrorCode("401");
            } else
            {
                if(_Debug)
                {
                    System.out.println("Warning - Optional Value Not Implemented");
                    System.out.println("Optional Element: " + element + " was not implemented");
                }
                dmErrorMgr.SetCurrentErrorCode("0");
            }
        }
        catch(NoSuchFieldException nsfe)
        {
            if(_Debug)
            {
                System.out.println("Error - Invalid Argument Error");
                System.out.println("Invalid data model element: " + element);
            }
            dmErrorMgr.SetCurrentErrorCode("201");
        }
        catch(SecurityException se)
        {
            if(_Debug)
            {
                System.out.println(se);
                System.out.println("Access to the information is denied");
            }
            dmErrorMgr.SetCurrentErrorCode("101");
        }
        catch(IllegalAccessException iae)
        {
            if(_Debug)
            {
                System.out.println(iae);
                System.out.println("Underlying constructor is inaccessible");
            }
            dmErrorMgr.SetCurrentErrorCode("101");
        }
        if(_Debug)
            System.out.println("Exiting CMICategory::doSet()");
    }

    protected boolean validateType(Object o, Element e, String element, String value, DMErrorManager dmErrorMgr)
    {
        if(_Debug)
        {
            System.out.println("   Validating data type of the set value");
            System.out.println("   Check method: " + e.getType());
            System.out.println("   Element: " + element);
            System.out.println("   Value: " + value);
        }
        boolean result = false;
        DataModelValidator dmValidator = new DataModelValidator();
        Class dmClass = dmValidator.getClass();
        Class parameterTypes[] = {
            e.getClass(), java.lang.String.class
        };
        Object arguments[] = {
            e, value
        };
        try
        {
            Method theMethod = dmClass.getMethod(e.getType(), parameterTypes);
            Boolean theResult = (Boolean)theMethod.invoke(dmValidator, arguments);
            result = theResult.booleanValue();
        }
        catch(NoSuchMethodException nsme)
        {
            if(_Debug)
            {
                System.out.println(nsme);
                System.out.println("No Such method: " + e.getType() + "()");
            }
            dmErrorMgr.SetCurrentErrorCode("101");
        }
        catch(IllegalAccessException iae)
        {
            if(_Debug)
                System.out.println(iae);
            dmErrorMgr.SetCurrentErrorCode("101");
        }
        catch(InvocationTargetException ite)
        {
            if(_Debug)
                System.out.println(ite);
            dmErrorMgr.SetCurrentErrorCode("101");
        }
        if(_Debug)
            System.out.println("   Result from validation: " + result);
        return result;
    }

    private static final long serialVersionUID = 0x59e5cb1a1e3e148cL;
    private static boolean _Debug;
    protected boolean writeable;
    static Class class$0; /* synthetic field */

    static 
    {
        _Debug = DebugIndicator.ON;
    }
}
